package com.xhwl.centiotdata.suppliersystem.vehicle.ketop;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.xhwl.common.dto.cent.vehicle.http.ketop.*;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryAccessRecordKeTopDTO;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryCommonKeTopDTO;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryFreeSpaceNumberKeTopDTO;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryPaymentKeTopDTO;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleParkingInfoBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class KeTopHandler {

    @Autowired
    private KeTopSignHandler keTopSignHandler;

    private void keTopResponseHandle(HttpResponse httpResponse, ResponseCommonKeTopDTO responseCommonKeTopDTO) {
        int status = httpResponse.getStatus();
        if (200!=status) {
            throw new BusinessException("请求科拓api接口，返回的http状态码不是200");
        }

        String resCode = responseCommonKeTopDTO.getResCode();
        if ("1101".equals(resCode)) {
            throw new BusinessException("请求科拓api接口，验签失败，认证不成功");
        }
        if ("1201".equals(resCode)) {
            throw new BusinessException("请求科拓api接口，请求参数不完整");
        }
        if (!"0".equals(resCode)) {
            throw new BusinessException("请求科拓api接口，返回的resCode（科拓）不为0，请求失败");
        }
    }

    public FreeSpaceNumberKeTopDTO queryFreeSpace(VehicleParkingInfoBO vehicleParkingInfoBO, QueryFreeSpaceNumberKeTopDTO queryFreeSpaceNumberKeTopDTO) {
        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetFreeSpaceNum";
        QueryFreeSpaceNumberKeTopDTO query = keTopSignHandler.sign(queryFreeSpaceNumberKeTopDTO, vehicleParkingInfoBO, "getFreeSpaceNum");
        HttpResponse httpResponse = HttpRequest.post(url)
                .header("version", vehicleParkingInfoBO.getVersion())
                .body(JSON.toJSONString(query))
                .execute();

        ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
        keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
        return JSON.parseObject(responseCommonKeTopDTO.getData(), FreeSpaceNumberKeTopDTO.class);
    }

    public ParkingLotInfoKeTopDTO queryParkingLotInfo(VehicleParkingInfoBO vehicleParkingInfoBO, QueryCommonKeTopDTO queryCommonKeTopDTO) {
        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetParkingLotInfo";
        QueryCommonKeTopDTO query = keTopSignHandler.sign(queryCommonKeTopDTO, vehicleParkingInfoBO, "getParkingLotInfo");
        HttpResponse httpResponse = HttpRequest.post(url)
                .header("version", vehicleParkingInfoBO.getVersion())
                .body(JSON.toJSONString(query))
                .execute();

        ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
        keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
        return JSON.parseObject(responseCommonKeTopDTO.getData(), ParkingLotInfoKeTopDTO.class);
    }

    public ParkingLotAreaKeTopDTO queryParkingLotArea(VehicleParkingInfoBO vehicleParkingInfoBO, QueryCommonKeTopDTO queryCommonKeTopDTO) {
        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetParkingPlaceArea";
        QueryCommonKeTopDTO query = keTopSignHandler.sign(queryCommonKeTopDTO, vehicleParkingInfoBO, "getParkingPlaceArea");
        HttpResponse httpResponse = HttpRequest.post(url)
                .header("version", vehicleParkingInfoBO.getVersion())
                .body(JSON.toJSONString(query))
                .execute();

        ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
        keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
        return JSON.parseObject(responseCommonKeTopDTO.getData(), ParkingLotAreaKeTopDTO.class);
    }

    public ParkingLotPassageKeTopDTO queryParkingLotPassage(VehicleParkingInfoBO vehicleParkingInfoBO, QueryCommonKeTopDTO queryCommonKeTopDTO) {
        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetParkingNode";
        QueryCommonKeTopDTO query = keTopSignHandler.sign(queryCommonKeTopDTO, vehicleParkingInfoBO, "getParkingNode");
        HttpResponse httpResponse = HttpRequest.post(url)
                .header("version", vehicleParkingInfoBO.getVersion())
                .body(JSON.toJSONString(query))
                .execute();

        ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
        keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
        return JSON.parseObject(responseCommonKeTopDTO.getData(), ParkingLotPassageKeTopDTO.class);
    }

    public List<AccessRecordKeTopDTO> getCarInOutInfo(VehicleParkingInfoBO vehicleParkingInfoBO, QueryAccessRecordBO queryAccessRecordBO, Integer inOut) {
        List<AccessRecordKeTopDTO> accessRecordKeTopDTOList = new ArrayList<>();

        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetCarInoutInfo";
        Integer totalCount;
        Integer pageIndex = 1;
        Integer pageSize;
        do {
            QueryAccessRecordKeTopDTO queryCommonKeTopDTO = new QueryAccessRecordKeTopDTO();
            queryCommonKeTopDTO.setStartTime(DateUtil.formatDateTime(queryAccessRecordBO.getStartTime()));
            queryCommonKeTopDTO.setEndTime(DateUtil.formatDateTime(queryAccessRecordBO.getEndTime()));
            queryCommonKeTopDTO.setCapFlag(inOut);
            queryCommonKeTopDTO.setPageIndex(pageIndex);
            pageSize = queryCommonKeTopDTO.getPageSize();

            QueryCommonKeTopDTO query = keTopSignHandler.sign(queryCommonKeTopDTO, vehicleParkingInfoBO, "getCarInoutInfo");
            HttpResponse httpResponse = HttpRequest.post(url)
                    .header("version", vehicleParkingInfoBO.getVersion())
                    .body(JSON.toJSONString(query))
                    .execute();

            ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
            keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
            AccessRecordKeTopDTO accessRecordKeTopDTO = JSON.parseObject(responseCommonKeTopDTO.getData(), AccessRecordKeTopDTO.class);
            accessRecordKeTopDTOList.add(accessRecordKeTopDTO);

            totalCount = accessRecordKeTopDTO.getTotalCount();
        } while (totalCount > ((pageIndex++) * pageSize));

        return accessRecordKeTopDTOList;
    }

    public List<PayRecordKeTopDTO> getParkingPaymentList(VehicleParkingInfoBO vehicleParkingInfoBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<PayRecordKeTopDTO> payRecordKeTopDTOList = new ArrayList<>();
        QueryPaymentKeTopDTO queryPaymentKeTopDTO = new QueryPaymentKeTopDTO();
        queryPaymentKeTopDTO.setStartTime(DateUtil.formatDateTime(queryAccessRecordBO.getStartTime()));
        queryPaymentKeTopDTO.setEndTime(DateUtil.formatDateTime(queryAccessRecordBO.getEndTime()));

        String url = vehicleParkingInfoBO.getHost() + "/" + "api/wec/GetParkingPaymentList";

        Integer totalCount;
        Integer pageIndex;
        Integer pageSize = queryPaymentKeTopDTO.getPageSize();

        do{
            QueryCommonKeTopDTO query = keTopSignHandler.sign(queryPaymentKeTopDTO, vehicleParkingInfoBO, "getParkingPaymentList");
            HttpResponse httpResponse = HttpRequest.post(url)
                    .header("version", vehicleParkingInfoBO.getVersion())
                    .body(JSON.toJSONString(query))
                    .execute();

            ResponseCommonKeTopDTO responseCommonKeTopDTO = JSON.parseObject(httpResponse.body(), ResponseCommonKeTopDTO.class);
            keTopResponseHandle(httpResponse, responseCommonKeTopDTO);
            PayRecordKeTopDTO payRecordKeTopDTO = JSON.parseObject(responseCommonKeTopDTO.getData(), PayRecordKeTopDTO.class);
            payRecordKeTopDTOList.add(payRecordKeTopDTO);

            totalCount = payRecordKeTopDTO.getTotalCount();
            pageIndex = payRecordKeTopDTO.getPageIndex();
        } while (totalCount > pageIndex * pageSize);

        return payRecordKeTopDTOList;
    }

}
